#-*- coding: utf-8 -*-
import pygame
import sys
from sys import exit
from pygame.locals import *
import random
import os
import time
  
# 设置游戏屏幕大小
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800
 
# 子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self, bullet_img, init_pos, speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet_img
        self.rect = self.image.get_rect()
        self.rect.midbottom = init_pos
        self.speed = speed

    def move(self):
        self.rect.top -= self.speed

    def moveReverse(self):
        self.rect.top += self.speed

    def moveLeft(self):
        self.rect.top -= self.speed
        self.rect.left -= self.speed / 2

    def moveRight(self):
        self.rect.top -= self.speed
        self.rect.left += self.speed / 2

# 玩家飞机类
class Player(pygame.sprite.Sprite):
    def __init__(self, plane_img, player_rect, init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = []                                 # 用来存储玩家飞机图片的列表
        for i in range(len(player_rect)):
            self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())
        self.rect = player_rect[0]                      # 初始化图片所在的矩形
        self.rect.topleft = init_pos                    # 初始化矩形的左上角坐标
        self.speed = 8                                  # 初始化玩家飞机速度，这里是一个确定的值
        self.bullets = pygame.sprite.Group()            # 玩家飞机所发射的子弹的集合
        self.bullets_left = pygame.sprite.Group()
        self.bullets_right = pygame.sprite.Group()
        self.is_hit = False                             # 玩家是否被击中

    # 发射子弹
    def shoot(self, bullet_img):
        bullet = Bullet(bullet_img, self.rect.midtop, 10)
        self.bullets.add(bullet)


    def shootLeft(self, bullet_img):
        bullet = Bullet(bullet_img, self.rect.midtop, 10)
        self.bullets_left.add(bullet)


    def shootRight(self, bullet_img):
        bullet = Bullet(bullet_img, self.rect.midtop, 10)
        self.bullets_right.add(bullet)


    # 向上移动，需要判断边界
    def moveUp(self):
        if self.rect.top <= 0:
            self.rect.top = 0
        else:
            self.rect.top -= self.speed

    # 向下移动，需要判断边界
    def moveDown(self):
        if self.rect.top >= SCREEN_HEIGHT - self.rect.height:
            self.rect.top = SCREEN_HEIGHT - self.rect.height
        else:
            self.rect.top += self.speed

    # 向左移动，需要判断边界
    def moveLeft(self):
        if self.rect.left <= 0:
            self.rect.left = 0
        else:
            self.rect.left -= self.speed

    # 向右移动，需要判断边界        
    def moveRight(self):
        if self.rect.left >= SCREEN_WIDTH - self.rect.width:
            self.rect.left = SCREEN_WIDTH - self.rect.width
        else:
            self.rect.left += self.speed

# 敌机类
class Enemy(pygame.sprite.Sprite):
    def __init__(self, enemy_img, enemy_down_rect, init_pos, speed, bulletSpeed, plane_img):
       pygame.sprite.Sprite.__init__(self)
       self.image = enemy_img
       self.rect = self.image.get_rect()
       self.rect.topleft = init_pos
       self.down_imgs = []
       for i in range(len(enemy_down_rect)):
           self.down_imgs.append(plane_img.subsurface(enemy_down_rect[i]).convert_alpha())
       self.speed = speed
       self.bulletSpeed = bulletSpeed
       self.bullets = pygame.sprite.Group()
       self.positionFlag = True

    # 敌机移动，边界判断及删除在游戏主循环里处理
    def move(self):
        self.rect.top += self.speed

    def moveLeft(self):
        if self.rect.left <= 0:
            self.rect.left = 0
        else:
            self.rect.left -= self.speed

    def moveRight(self):
        if self.rect.left >= SCREEN_WIDTH - self.rect.width:
            self.rect.left = SCREEN_WIDTH - self.rect.width
        else:
            self.rect.left += self.speed


    def shoot(self, bullet_img):
        bullet = Bullet(bullet_img, self.rect.midbottom, self.bulletSpeed)
        self.bullets.add(bullet)


def get_resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)

class AirGameController():

    def __init__(self):
        self.reset()


    def reset(self):
        pygame.init()
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

        # 游戏界面标题
        pygame.display.set_caption('StarCraft')

        # 背景图
        self.background = pygame.image.load(get_resource_path('resources/image/background.png')).convert()

        # Game Over 的背景图
        self.game_over = pygame.image.load(get_resource_path('resources/image/gameover.png'))

        # 飞机及子弹图片集合
        self.plane_img = pygame.image.load(get_resource_path('resources/image/shoot.png'))
        self.mask_plane = pygame.mask.from_surface(self.plane_img)

        # 设置玩家飞机不同状态的图片列表，多张图片展示为动画效果
        self.player_rect = []
        self.player_rect.append(pygame.Rect(0, 100, 100, 115))        # 玩家飞机图片
        # player_rect.append(pygame.Rect(0, 0, 70, 90))
        self.player_rect.append(pygame.Rect(165, 235, 100, 115))     # 玩家爆炸图片

        self.player_pos = [200, 600]
        self.player = Player(self.plane_img, self.player_rect, self.player_pos)

        # 子弹图片
        self.bullet_rect_player = pygame.Rect(68, 77, 9, 18)
        self.bullet_img_player = self.plane_img.subsurface(self.bullet_rect_player)

        self.bullet_rect_enemy = pygame.Rect(1004, 987, 9, 18)
        self.bullet_img_enemy = self.plane_img.subsurface(self.bullet_rect_enemy)
        self.bullet_mask_enemy = pygame.mask.from_surface(self.bullet_img_enemy)

        self.bullet_rect_boss = pygame.Rect(275, 250, 43, 42)
        # self.bullet_rect_boss = pygame.Rect(100, 120, 60, 105)
        self.bullet_img_boss = self.plane_img.subsurface(self.bullet_rect_boss)
        self.bullet_mask_boss = pygame.mask.from_surface(self.bullet_img_boss)

        # 敌机不同状态的图片列表，包括正常敌机，爆炸的敌机图片
        self.enemy1_rect = pygame.Rect(534, 612, 53, 38)
        self.enemy1_down_rect = []
        self.enemy1_down_rect.append(pygame.Rect(267, 347, 53, 38))
        # self.enemy1_down_rect = pygame.Rect(267, 347, 53, 38)
        self.enemy1_img = self.plane_img.subsurface(self.enemy1_rect)
        # self.enemy1_down_imgs = self.plane_img.subsurface(self.enemy1_down_rect)
        self.enemy1_mask = pygame.mask.from_surface(self.enemy1_img)

        self.enemy2_rect = pygame.Rect(167, 765, 167, 255)
        # self.enemy2_down_rect = pygame.Rect(835, 765, 167, 255)
        self.enemy2_down_rect = []
        self.enemy2_down_rect.append(pygame.Rect(835, 765, 167, 255))
        self.enemy2_img = self.plane_img.subsurface(self.enemy2_rect)
        # self.enemy2_down_imgs = self.plane_img.subsurface(self.enemy2_down_rect)
        self.enemy2_mask = pygame.mask.from_surface(self.enemy2_img)

        self.enemy3_rect = pygame.Rect(0, 0, 68, 90)
        # self.enemy3_down_rect = pygame.Rect(668, 656, 72, 86)
        self.enemy3_down_rect = []
        self.enemy3_down_rect.append(pygame.Rect(524, 656, 72, 86))
        self.enemy3_down_rect.append(pygame.Rect(596, 656, 72, 86))
        self.enemy3_down_rect.append(pygame.Rect(668, 656, 72, 86))

        self.enemy3_img = self.plane_img.subsurface(self.enemy3_rect)
        # self.enemy3_down_imgs = self.plane_img.subsurface(self.enemy3_down_rect)
        self.enemy3_mask = pygame.mask.from_surface(self.enemy3_img)

        self.enhanceBom_rect = pygame.Rect(267, 399, 60, 90)
        # self.enemy4_down_rect = pygame.Rect(668, 656, 72, 86)
        self.enhanceBom_down_rect = []
        self.enhanceBom_down_rect.append(pygame.Rect(668, 656, 72, 86))
        self.enhanceBom_img = self.plane_img.subsurface(self.enhanceBom_rect)
        # self.enhanceBom_down_img = self.plane_img.subsurface(self.enemy4_down_rect)
        self.enhanceBom_mask = pygame.mask.from_surface(self.enhanceBom_img)

        self.enhanceBom2_rect = pygame.Rect(100, 120, 60, 105)
        # self.enemy4_down_rect = pygame.Rect(668, 656, 72, 86)
        self.enhanceBom2_down_rect = []
        self.enhanceBom2_down_rect.append(pygame.Rect(668, 656, 72, 86))
        self.enhanceBom2_img = self.plane_img.subsurface(self.enhanceBom2_rect)
        # self.enhanceBom_down_img = self.plane_img.subsurface(self.enemy4_down_rect)
        self.enhanceBom2_mask = pygame.mask.from_surface(self.enhanceBom2_img)


        #存储敌机，管理多个对象
        self.enemies1 = pygame.sprite.Group()
        self.enemies2 = pygame.sprite.Group()
        self.enemies3 = pygame.sprite.Group()
        self.enhanceBoms = pygame.sprite.Group()
        self.enhanceBoms2 = pygame.sprite.Group()

        # 存储被击毁的飞机
        self.enemies_down = pygame.sprite.Group()

        # 初始化射击及敌机移动频率
        self.shoot_frequency = 0
        self.shoot_frequency_level = 15
        self.shoot_frequency_enemy = 0
        self.shoot_frequency_boss = 0
        self.enemy_frequency = 0
        # enemyBig_frequency = 0

        #enemy life:
        self.enemy1_life = 1
        self.enemy2_life = 30
        self.enemy3_life = 3

        # 初始化分数
        self.score = 0

        # 游戏循环帧率设置
        self.clock = pygame.time.Clock()

        # 定义游戏结果
        self.result = True
        self.superShoot = False

    def runningGame(self):
        # 控制游戏最大帧率为 60
        self.clock.tick(60)

        # 生成子弹，需要控制发射频率
        # 首先判断玩家飞机没有被击中
        # 循环15次发射一个子弹
        if not self.player.is_hit:
            if self.shoot_frequency % self.shoot_frequency_level == 0:
                self.player.shoot(self.bullet_img_player)
                if self.superShoot == True:
                    self.player.shootLeft(self.bullet_img_player)
                    self.player.shootRight(self.bullet_img_player)
            self.shoot_frequency += 1
            if self.shoot_frequency >= self.shoot_frequency_level:
                self.shoot_frequency = 0

        # 生成敌机，需要控制生成频率
        # 循环50次生成一架敌机
        if self.enemy_frequency % int(120 - self.enemy_frequency / 100)== 0:
            self.enemy1_pos = [random.randint(0, SCREEN_WIDTH - self.enemy1_rect.width), 0]
            self.enemy1 = Enemy(self.enemy1_img, self.enemy1_down_rect, self.enemy1_pos, 3*(random.random())+1, 5, self.plane_img) #+enemy_frequency*0.005)
            self.enemies1.add(self.enemy1)

        if (self.enemy_frequency % 3500 == 0) and (self.enemy_frequency > 0):
            self.enemy2_pos = [random.randint(0, SCREEN_WIDTH - self.enemy2_rect.width), 0]
            # set enemy's life
            for i in range(1):
                self.enemy2 = Enemy(self.enemy2_img, self.enemy2_down_rect, self.enemy2_pos, 1, 3, self.plane_img)
                self.enemies2.add(self.enemy2)

        if (self.enemy_frequency % 377 == 0) and (self.enemy_frequency > 0):
            self.enemy3_pos = [random.randint(0, SCREEN_WIDTH - self.enemy3_rect.width), 0]
            # set enemy's life
            for i in range(1):
                self.enemy3 = Enemy(self.enemy3_img, self.enemy3_down_rect, self.enemy3_pos, 1, 5, self.plane_img)
                self.enemies3.add(self.enemy3)

        if (self.enemy_frequency % 800 == 0) and (self.enemy_frequency > 0):
            self.enhanceBom_pos = [random.randint(0, SCREEN_WIDTH - self.enhanceBom_rect.width), 0]
            self.enhanceBom = Enemy(self.enhanceBom_img, self.enhanceBom_down_rect, self.enhanceBom_pos, 1.5, 5, self.plane_img)
            self.enhanceBoms.add(self.enhanceBom)

        if (self.enemy_frequency % 1234 == 0) and (self.enemy_frequency > 0):
            self.enhanceBom2_pos = [random.randint(0, SCREEN_WIDTH - self.enhanceBom2_rect.width), 0]
            self.enhanceBom2 = Enemy(self.enhanceBom2_img, self.enhanceBom2_down_rect, self.enhanceBom2_pos, 1.5, 5, self.plane_img)
            self.enhanceBoms2.add(self.enhanceBom2)


        self.enemy_frequency += 1
        if self.enemy_frequency >= 12000:
            self.enemy_frequency = 0

        for bullet in self.player.bullets:
            # 以固定速度移动子弹
            bullet.move()
            # 移动出屏幕后删除子弹
            if bullet.rect.bottom < 0:
                self.player.bullets.remove(bullet)

        for bullet in self.player.bullets_left:
            bullet.moveLeft()
            if bullet.rect.bottom < 0 or bullet.rect.left < 0:
                self.player.bullets.remove(bullet)

        for bullet in self.player.bullets_right:
            bullet.moveRight()
            if bullet.rect.bottom < 0 or bullet.rect.left > SCREEN_WIDTH - bullet.rect.width:
                self.player.bullets.remove(bullet)

        for enemy in self.enemies1:
            #2. 移动敌机
            enemy.move()

            #3. 敌机与玩家飞机碰撞效果处理
            offset = self.player.rect.x - enemy.rect.x, self.player.rect.y - enemy.rect.y
            if self.enemy1_mask.overlap(self.mask_plane, offset) != None:
            # if pygame.sprite.collide_circle(enemy, player):
                self.enemies_down.add(enemy)
                self.enemies1.remove(enemy)
                self.player.is_hit = True
                print("Player hit by enemi1")
                break
            #4. 移动出屏幕后删除敌人
            if enemy.rect.top < 0:
                self.enemies1.remove(enemy)

        for enemy in self.enemies2:
            
            # if enemy_frequency % 200 == 0:
            if enemy.rect.left <=0 or enemy.rect.left >= SCREEN_WIDTH - enemy.rect.width:
                enemy.positionFlag = not enemy.positionFlag
            if enemy.positionFlag:
                enemy.moveRight()
            if not enemy.positionFlag:
                enemy.moveLeft()
            # enemyBig_frequency += 1
            # if enemyBig_frequency >= 50:
            #     enemyBig_frequency = 0
            # print("enemyBig_frequency:", enemyBig_frequency, "enemy.positionFlag", enemy.positionFlag)

            if self.shoot_frequency_boss % 120 == 0:
                enemy.shoot(self.bullet_img_boss)

            self.shoot_frequency_boss += 1
            if self.shoot_frequency_boss >= 120:
                self.shoot_frequency_boss = 0

            for bullet in enemy.bullets:
                bullet.moveReverse()
                if bullet.rect.top < 0:
                    enemy.bullets.remove(bullet)

                offset = self.player.rect.x - bullet.rect.x, self.player.rect.y - bullet.rect.y
                if self.bullet_mask_boss.overlap(self.mask_plane, offset) != None:
                # if pygame.sprite.collide_circle(bullet, player):
                    enemy.bullets.remove(bullet)
                    self.player.is_hit = True
                    print("player hit by boss bom")
                    break

            offset = self.player.rect.x - enemy.rect.x, self.player.rect.y - enemy.rect.y
            if self.enemy2_mask.overlap(self.mask_plane, offset) != None:
            # if pygame.sprite.collide_circle(enemy, player):
                self.enemies_down.add(enemy)
                self.enemies2.remove(enemy)
                self.player.is_hit = True
                print("player hit by boss")
                break

            if enemy.rect.top < 0:
                self.enemies2.remove(enemy)

        for enemy in self.enemies3:
            enemy.move()
            if self.shoot_frequency_enemy % 90 == 0:
                enemy.shoot(self.bullet_img_enemy)
            self.shoot_frequency_enemy += 1
            if self.shoot_frequency_enemy >= 90:
                self.shoot_frequency_enemy = 0

            for bullet in enemy.bullets:
                bullet.moveReverse()
                if bullet.rect.top < 0:
                    enemy.bullets.remove(bullet)

                # offset = self.player.rect.x - bullet.rect.x, self.player.rect.y - bullet.rect.y
                # if self.bullet_mask_enemy.overlap(self.mask_plane, offset) != None:
                if pygame.sprite.collide_circle(bullet, self.player):
                    enemy.bullets.remove(bullet)
                    self.player.is_hit = True
                    print("player hit by enemy3 bullet")
                    break
            
            # enemy.bullets.draw(screen)
            offset = self.player.rect.x - enemy.rect.x, self.player.rect.y - enemy.rect.y
            if self.enemy3_mask.overlap(self.mask_plane, offset) != None:
            # if pygame.sprite.collide_circle(enemy, player):
                self.enemies_down.add(enemy)
                self.enemies3.remove(enemy)
                self.player.is_hit = True
                print("player hit by enemy3")
                break

            if enemy.rect.top < 0:
                self.enemies3.remove(enemy)

        for enhanceBom in self.enhanceBoms:
            enhanceBom.move()
            # if enemy_frequency % 200 == 0:
            if enhanceBom.rect.left <=0 or enhanceBom.rect.left >= SCREEN_WIDTH - enhanceBom.rect.width:
                enhanceBom.positionFlag = not enhanceBom.positionFlag
            if enhanceBom.positionFlag:
                enhanceBom.moveRight()
            if not enhanceBom.positionFlag:
                enhanceBom.moveLeft()

            offset = self.player.rect.x - enhanceBom.rect.x, self.player.rect.y - enhanceBom.rect.y
            if self.enhanceBom_mask.overlap(self.mask_plane, offset) != None:
            # if pygame.sprite.collide_circle(enhanceBom, player):
                self.enhanceBoms.remove(enhanceBom)
                self.shoot_frequency_level -= 3
                if self.shoot_frequency_level <= 3:
                    self.shoot_frequency_level = 3
                    for bullet in self.player.bullets:
                        bullet.speed += 4
                    for bullet in self.player.bullets_right:
                        bullet.speed += 4
                    for bullet in self.player.bullets_left:
                        bullet.speed +=4

            #4. 移动出屏幕后删除敌人
            if enhanceBom.rect.top < 0:
                self.enhanceBoms.remove(enhanceBom)

        for enhanceBom in self.enhanceBoms2:
            enhanceBom.move()
            # if enemy_frequency % 200 == 0:
            if enhanceBom.rect.left <=0 or enhanceBom.rect.left >= SCREEN_WIDTH - enhanceBom.rect.width:
                enhanceBom.positionFlag = not enhanceBom.positionFlag
            if enhanceBom.positionFlag:
                enhanceBom.moveRight()
            if not enhanceBom.positionFlag:
                enhanceBom.moveLeft()

            offset = self.player.rect.x - enhanceBom.rect.x, self.player.rect.y - enhanceBom.rect.y
            if self.enhanceBom2_mask.overlap(self.mask_plane, offset) != None:
            # if pygame.sprite.collide_circle(enhanceBom, player):
                self.enhanceBoms2.remove(enhanceBom)
                self.superShoot = True

            #4. 移动出屏幕后删除敌人
            if enhanceBom.rect.top < 0:
                self.enhanceBoms2.remove(enhanceBom)

        #敌机被子弹击中效果处理
        #将被击中的敌机对象添加到击毁敌机 Group 中

        enemies1_down = pygame.sprite.groupcollide(self.enemies1, self.player.bullets, 1, 1)
        for enemy_down in enemies1_down:
            self.enemies_down.add(enemy_down)
        enemies2_down = pygame.sprite.groupcollide(self.enemies2, self.player.bullets, 0, 1)
        for enemy_down in enemies2_down:
            self.enemy2_life -= 1
            self.score += 1
            if self.enemy2_life == 0:
                self.enemies2.remove(enemy_down)
                self.enemies_down.add(enemy_down)
                self.enemy2_life = 15
        enemies3_down = pygame.sprite.groupcollide(self.enemies3, self.player.bullets, 0, 1)
        for enemy_down in enemies3_down:
            self.enemy3_life -= 1
            self.score += 1
            if self.enemy3_life == 0:
                self.enemies3.remove(enemy_down)
                self.enemies_down.add(enemy_down)
                self.enemy3_life = 5

        enemies1_down = pygame.sprite.groupcollide(self.enemies1, self.player.bullets_left, 1, 1)
        for enemy_down in enemies1_down:
            self.enemies_down.add(enemy_down)
        enemies2_down = pygame.sprite.groupcollide(self.enemies2, self.player.bullets_left, 0, 1)
        for enemy_down in enemies2_down:
            self.enemy2_life -= 1
            self.score += 1
            if self.enemy2_life == 0:
                self.enemies2.remove(enemy_down)
                self.enemies_down.add(enemy_down)
                self.enemy2_life = 15
        enemies3_down = pygame.sprite.groupcollide(self.enemies3, self.player.bullets_left, 0, 1)
        for enemy_down in enemies3_down:
            self.enemy3_life -= 1
            self.score += 1
            if self.enemy3_life == 0:
                self.enemies3.remove(enemy_down)
                self.enemies_down.add(enemy_down)
                self.enemy3_life = 5

        enemies1_down = pygame.sprite.groupcollide(self.enemies1, self.player.bullets_right, 1, 1)
        for enemy_down in enemies1_down:
            self.enemies_down.add(enemy_down)
        enemies2_down = pygame.sprite.groupcollide(self.enemies2, self.player.bullets_right, 0, 1)
        for enemy_down in enemies2_down:
            self.enemy2_life -= 1
            self.score += 1
            if self.enemy2_life == 0:
                self.enemies2.remove(enemy_down)
                self.enemies_down.add(enemy_down)
                self.enemy2_life = 15
        enemies3_down = pygame.sprite.groupcollide(self.enemies3, self.player.bullets_right, 0, 1)
        for enemy_down in enemies3_down:
            self.enemy3_life -= 1
            self.score += 1
            if self.enemy3_life == 0:
                self.enemies3.remove(enemy_down)
                self.enemies_down.add(enemy_down)
                self.enemy3_life = 5

        # 绘制背景
        self.screen.fill(0)
        self.screen.blit(self.background, (0, 0))

        # 绘制玩家飞机
        if not self.player.is_hit:
            self.screen.blit(self.player.image[0], self.player.rect) #将正常飞机画出来
        else:
            # 玩家飞机被击中后的效果处理
            self.screen.blit(self.player.image[1], self.player.rect) #将爆炸的飞机画出来
            # self.running = False
            self.result = False
            return self.result

        # 敌机被子弹击中效果显示
        for enemy_down in self.enemies_down:
            self.enemies_down.remove(enemy_down)
            self.score += 1
            for down_img in enemy_down.down_imgs:
                for i in range(60):
                    self.screen.blit(down_img, enemy_down.rect) #将爆炸的敌机画出来

        # 显示子弹
        self.player.bullets.draw(self.screen)
        self.player.bullets_left.draw(self.screen)
        self.player.bullets_right.draw(self.screen)
        # 显示敌机
        self.enemies1.draw(self.screen)
        self.enemies2.draw(self.screen)
        self.enemies3.draw(self.screen)
        self.enhanceBoms.draw(self.screen)
        self.enhanceBoms2.draw(self.screen)

        for enemy in self.enemies2:
            enemy.bullets.draw(self.screen)

        for enemy in self.enemies3:
            enemy.bullets.draw(self.screen)

        # 绘制得分
        score_font = pygame.font.Font(None, 36)
        score_text = score_font.render('score: '+str(self.score), True, (128, 128, 128))
        text_rect = score_text.get_rect()
        text_rect.topleft = [10, 10]
        self.screen.blit(score_text, text_rect)

        # 更新屏幕
        pygame.display.update()

        # 处理游戏退出
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

        # 获取键盘事件（上下左右按键）
        key_pressed = pygame.key.get_pressed()

        # 处理键盘事件（移动飞机的位置）
        if key_pressed[K_w] or key_pressed[K_UP]:
            self.player.moveUp()
        if key_pressed[K_s] or key_pressed[K_DOWN]:
            self.player.moveDown()
        if key_pressed[K_a] or key_pressed[K_LEFT]:
            self.player.moveLeft()
        if key_pressed[K_d] or key_pressed[K_RIGHT]:
            self.player.moveRight()
        if key_pressed[K_p]:
            self.shoot_frequency_level = 3
            self.superShoot = True


    def showText(self, content, textStyle, textSize, red, yellow, blue, horiDiff, verDiff):

        font = pygame.font.Font(pygame.font.match_font(textStyle), textSize)
        text = font.render(content, True, (red, yellow, blue))
        text_rect = text.get_rect()
        text_rect.centerx = self.screen.get_rect().centerx + horiDiff
        text_rect.centery = self.screen.get_rect().centery + verDiff
        self.screen.blit(text, text_rect)


    def startGame(self):
        self.showText('StarCraft', 'phosphate', 60, 108, 106, 255, 0, -200)
        self.showText('Press Space To Start', 'phosphate', 32, 108, 106, 255, 0, 200)
        self.showText('Press i for game instructions', 'futura', 20, 108, 106, 255, 0, 350)

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit()
            pygame.display.update()

            if pygame.key.get_pressed()[K_SPACE]:
                pygame.init()
                self.reset()
                while self.result:
                    self.runningGame()
                self.gameOver()

            if pygame.key.get_pressed()[K_i]:
                pygame.init()
                self.reset()
                self.instructionPage()


    def instructionPage(self):
        self.showText('Instructions:                 ', 'futura', 30, 108, 106, 255, 0, -200)
        self.showText('w/up: move up', 'futura', 30, 108, 106, 255, 0, -150)
        self.showText('s/down: move down', 'futura', 30, 108, 106, 255, 0, -100)
        self.showText('a/left: move left', 'futura', 30, 108, 106, 255, 0, -50)
        self.showText('d/right: move right', 'futura', 30, 108, 106, 255, 0, 0)
        self.showText('Cheating:                   ', 'futura', 30, 108, 106, 255, 0, 100)
        self.showText('p: power mode', 'futura', 30, 108, 106, 255, 0, 150)
        self.showText('Press ESC to return main menu', 'futura', 20, 108, 106, 255, 0, 350 )

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit()
            pygame.display.update()

            if pygame.key.get_pressed()[K_ESCAPE]:
                pygame.init()
                self.reset()
                self.startGame()


    def gameOver(self):
        # 游戏 Game Over 后显示最终得分
        self.screen.blit(self.game_over, (0, 0))

        self.showText('Final Score: '+ str(self.score), 'phosphate', 50, 255, 0, 0, 0, 80)
        self.showText('Press Space To Restart', 'phosphate', 32, 255, 0, 0, 0, 200)

        # 显示得分并处理游戏退出
        while True:
            # print("waiting for restart")
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit()
            pygame.display.update()

            if pygame.key.get_pressed()[K_SPACE]:
                pygame.init()
                self.reset()
                while self.result:
                    self.runningGame()
                self.gameOver()



if __name__ == "__main__":   
    AirGameController = AirGameController()
    AirGameController.startGame()



